
const { ProcessMgr } = require("./processMgr");
const { SIGNAL_DEFINE } = require("./signalDefines");

const RobBankerType = {
    ROB: 0,     // 每轮都抢
    RANDOM: 1,  // 每轮都延续
    ROB_WINNER: 2, // 第一轮抢庄，后面是胜利者当庄
};

class RobBankerProcessMgr extends ProcessMgr {
    constructor () {
        super();
        this.robBankerTime = 0;
        this.decideBankerTime = 0;
        this.timerId = null;
        this.banker = null;
        this.scoreRobs = {};
        this.room = null;
    }

    setRobBankerTime(robBankerTime) {
        this.robBankerTime = robBankerTime;
    }

    setDecideBankerTime(decideBankerTime) {
        this.decideBankerTime = decideBankerTime;
    }

    setRobBankerType(type) {
        this.robBankerType = type;
    }

    getRobBankerTime() {
        return this.robBankerTime;
    }

    doAction() {
        if (this.robBankerType == RobBankerType.ROB) {
            this.robBanker();
        } else if (this.robBankerType == RobBankerType.RANDOM) {
            this.randomBanker();
        } else if (this.robBankerType == RobBankerType.ROB_WINNER) {
            if (this.room.isFirstRound()) {
                this.robBanker();
            } else {
                this.banker = this.room.getWinner();
                // 通知定庄不播动画
                this.showDecideBanker([this.banker], false);
            }
        }
    }

    robBanker() {
        this.timerId = setTimeout(function () {
            this.decideBanker();
        }, this.robBankerTime * 1000);
    }

    randomBanker() {
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        var random = Math.floor(Math.random() * gamberIndexs.length);
        this.banker = gamberIndexs[random];
        this.showDecideBanker(gamberIndexs);
    }

    showDecideBanker(robGambers) {
        var needShowRob = robGambers.length > 1;
        if (needShowRob) {
            this.timerId = setTimeout(function () {
                this.doNextAction();
            }, this.decideBankerTime * 1000);
        } else {
            this.doNextAction();
        }
        var userId = this.seatMgr.getUserIdBySeatIndex(this.banker);
        this.signal.emit(SIGNAL_DEFINE.DECIDE_BANKER, userId, robGambers);
    }

    decideBanker() {
        if (this.timerId) {
            clearTimeout(this.timerId);
        }
        
        var maxRobScore = 0;
        var robGambers = [];
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (let i = 0; i < gamberIndexs.length; ++i) {
            var score = this.scoreRobs[gamberIndexs[i]];
            if (score > maxRobScore) {
                maxRobScore = score;
                robGambers = [gamberIndexs[i]];
            } else if (score == maxRobScore) {
                robGambers.push(gamberIndexs[i]);
            }
        }

        if (robGambers.length == 1) {
            this.banker = robGambers[0];
            this.showDecideBanker(robGambers);
        } else {
            if (robGambers.length > 0) {    // 多个抢，随机一个
                var random = Math.floor(Math.random() * robGambers.length);
                this.banker = robGambers[random];
                this.showDecideBanker(robGambers);
            } else {
                this.randomBanker();    // 都不抢，就随机
            }
        }
    }

    onRobBanker(seatIndex, value) {
        this.scoreRobs[seatIndex] = value;

        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (let i = 0; i < gamberIndexs.length; ++i) {
            if (this.scoreRobs[gamberIndexs[i]] == null) {
                return;
            }
        }
        this.decideBanker();
    }
    getBanker() {
        return this.banker;
    }
}

exports.RobBankerType = RobBankerType;
exports.RobBankerProcessMgr = RobBankerProcessMgr;